package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.CommentMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.CommentService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Comment> getCommentList(Integer articleId) {
        return commentMapper.getCommentList(articleId);
    }

    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    @Override
    public List<SubComment> getSubCommentList() {
        return commentMapper.getSubCommentList();
    }

    @Override
    public void addSub(SubComment subComment) {
        commentMapper.addSub(subComment);
    }

    @Override
    public PageBean<Comment> manageComment(Integer pageNum, Integer pageSize, String content) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        PageBean<Comment> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        Integer userId = (Integer)claims.get("id");
        String role = (String)claims.get("role");
        List<Comment> commentList;
        if (role.equals("user")){
            commentList = commentMapper.manageComment(content,userId);
        }else {
            commentList = commentMapper.manageComment(content,null);
        }

        List<User> allUserInfo = userMapper.getAllUserInfo();
        for (Comment comment:commentList){
            for (User user:allUserInfo){
                if (comment.getCreateUser().equals(user.getId())){
                    comment.setUsername(user.getNickname());
                }
            }
        }
        Page<Comment> page = (Page<Comment>) commentList;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    @Override
    public void deleteComment(Integer id) {
        commentMapper.deleteComment(id);
    }

    public void deleteSubComment(Integer id) {
        commentMapper.deleteSubComment(id);
    }

    @Override
    public PageBean<SubComment> manageSubComment(Integer pageNum, Integer pageSize, String content) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        PageBean<SubComment> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        Integer userId = (Integer)claims.get("id");
        String role = (String)claims.get("role");
        List<SubComment> subcommentList;
        if (role.equals("user")){
            subcommentList = commentMapper.manageSubComment(content,userId);
        }else {
            subcommentList = commentMapper.manageSubComment(content,null);
        }

        List<User> allUserInfo = userMapper.getAllUserInfo();
        for (SubComment subcomment:subcommentList){
            for (User user:allUserInfo){
                if (subcomment.getCreateUser().equals(user.getId())){
                    subcomment.setUsername(user.getNickname());
                }
            }
        }
        Page<SubComment> page = (Page<SubComment>) subcommentList;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }
}
